package com.luyuan.crm.sale.web.provider;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.faces.model.SelectItem;

import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.Customer;
import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.member.model.NoAccountCustomer;
import com.luyuan.crm.member.service.MemberService;
import com.luyuan.crm.sale.web.BaseOrderWizardAction;
import com.ruijie.blueprint.spring.context.ApplicationContextHolder;
import com.ruijie.web.jsf.action.WizardAccessor;
import com.ruijie.web.jsf.provider.SelectItemsProvider;

@Component("accountMemberProvider")
public class AccountMemberProvider implements SelectItemsProvider {

    @Resource(name = "memberService")
    private MemberService memberService;

    @Override
    public List<SelectItem> getSelectItems(Object... parameters) {
        List<SelectItem> items = new ArrayList<SelectItem>();

        WizardAccessor wizardAccessor = (WizardAccessor) ApplicationContextHolder.getApplicationContext().getBean(
                "wizardAccessor");
        BaseOrderWizardAction action = (BaseOrderWizardAction) wizardAccessor.getWizardSteps().get(0).getAction();
        Customer customer = action.getOrder().getCustomer();
        items.add(new SelectItem(customer, customer.getName()));

        if (!(customer instanceof Member)) {
            return items;
        }

        Member member = memberService.getById((String) customer.getId());
        Set<NoAccountCustomer> customers = member.getAccountMembers();
        for (Customer customer2 : customers) {
            items.add(new SelectItem(customer2, customer2.getName()));
        }

        return items;
    }

}
